package com.csw.shuanfa.bitset;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class ShuffListMethodSortListRemove {
    public static int num = 2;
    static Random random = new Random();

    public static void main(String[] args) {
        Long start = System.currentTimeMillis();
        int num = (int) Math.pow(10, 4);
        List<String> cardList = new ArrayList<>();
        for (int i = 0; i < Math.pow(10, 6); i++) {
            cardList.add("cardId" + i);
        }


        // 提供一个方法,随机从cardList中取出num数量的元素,并且新列表的元素顺序和保持和原列表一致(即:原本在前的还在前边)
        List<String> randomList = shuffleMethod(cardList, num);
        ////randomList);
        Long end = System.currentTimeMillis();
        //(end - start) + "ms");
    }

    /**
     * 随机拿到配置num数量的数据
     */
    private static List<String> shuffleMethod(List<String> inputList, int num) {

        num = num <= inputList.size() ? num : inputList.size();

        //存下表
        List<Integer> weiZhi = new ArrayList<>();
        for (int i = 0; i < inputList.size(); i++) {
            weiZhi.add(i);
        }
        //存获取的下标，按照先后顺序来
        List<Integer> list = new ArrayList();
        for (int i = 0; i < num; i++) {
            int xiaBiao = random.nextInt(weiZhi.size());
            list.add(weiZhi.get(xiaBiao));
            weiZhi.remove(weiZhi.get(xiaBiao));
        }

        Collections.sort(list);
        List<String> resList = new ArrayList<>();

        for (int index : list) {
            resList.add(inputList.get(index));
        }
        return resList;
    }

}
